package com.zyiot.lbs;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.waps.AppConnect;

import com.umeng.analytics.MobclickAgent;
import com.umeng.fb.FeedbackAgent;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.common.SocializeConstants;
import com.umeng.socialize.controller.RequestType;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.sso.TencentWBSsoHandler;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;
import com.zyiot.Dao.ReplysDao;
import com.zyiot.model.Replys;

public class TabMoreActivity extends Activity {
	private Context mContext;
	InitApplication app;

	TextView suggestReplay;
	ReplysDao replysdao;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.layout_more);
		app = (InitApplication) getApplication();
		app.agent = new FeedbackAgent(mContext);
		suggestReplay = (TextView) findViewById(R.id.suggestReplay);

		replysdao = new ReplysDao(mContext);
		if (replysdao.getValue().equals("1")) {
			suggestReplay.setVisibility(View.VISIBLE);
		}

		RelativeLayout btnusercenter = (RelativeLayout) findViewById(R.id.button_user_center);
		btnusercenter.setOnClickListener(btnusercenter_listener);

		RelativeLayout btnmoretuan = (RelativeLayout) findViewById(R.id.button_more_tuan);
		btnmoretuan.setOnClickListener(btnmoretuan_listener);

		RelativeLayout btnmoreapp = (RelativeLayout) findViewById(R.id.button_more_app);
		btnmoreapp.setOnClickListener(btnmoreapp_listener);

		RelativeLayout btnfeedback = (RelativeLayout) findViewById(R.id.button_feedback);
		btnfeedback.setOnClickListener(btnfeedback_listener);

		RelativeLayout btncheckupdate = (RelativeLayout) findViewById(R.id.button_check_update);
		btncheckupdate.setOnClickListener(btncheckupdate_listener);

		RelativeLayout btnabout = (RelativeLayout) findViewById(R.id.button_about);
		btnabout.setOnClickListener(btnabout_listener);

		PackageInfo pinfo;
		String versionName = null;
		try {
			pinfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_CONFIGURATIONS);
			versionName = pinfo.versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}

		TextView tv_revsion = (TextView) findViewById(R.id.tv_revsion);
		tv_revsion.setText("当前版本:" + versionName);

	}

	OnClickListener btnusercenter_listener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			UMSocialService controller = UMServiceFactory.getUMSocialService("ANDROID_LOGIN", RequestType.SOCIAL);
			int flag = SocializeConstants.FLAG_USER_CENTER_LOGIN_VERIFY | SocializeConstants.FLAG_USER_CENTER_HIDE_LOGININFO;
			controller.getConfig().setDefaultShareLocation(false);
			// controller.getConfig().setPlatforms(SHARE_MEDIA.SINA,
			// SHARE_MEDIA.QZONE, SHARE_MEDIA.TENCENT, SHARE_MEDIA.RENREN);
			// 添加腾讯微博SSO支持
			controller.getConfig().setSsoHandler(new TencentWBSsoHandler());
			
			controller.getConfig().setPlatforms(SHARE_MEDIA.TENCENT);

			controller.openUserCenter(mContext, flag);
		}
	};

	OnClickListener btnmoretuan_listener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			AppConnect.getInstance(mContext).showTuanOffers(mContext);
		}
	};
	OnClickListener btnmoreapp_listener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(mContext, MoreAppActivity.class);
			startActivity(intent);
		}
	};

	OnClickListener btnfeedback_listener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			app.agent.startFeedbackActivity();
			Replys reply = new Replys();
			reply.setRep_state("0");
			replysdao.update(reply);
			suggestReplay.setVisibility(View.GONE);
			// AppConnect.getInstance(context).showFeedback();
		}
	};

	OnClickListener btncheckupdate_listener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			UmengUpdateAgent.setUpdateOnlyWifi(false); // 目前我们默认在Wi-Fi接入情况下才进行自动提醒。如需要在其他网络环境下进行更新自动提醒，则请添加该行代码
			UmengUpdateAgent.setUpdateAutoPopup(false);
			UmengUpdateAgent.setUpdateListener(updateListener);
			UmengUpdateAgent.update(mContext);

		}
	};

	OnClickListener btnabout_listener = new OnClickListener() {
		@Override
		public void onClick(View v) {

			Intent intent = new Intent();
			intent.setClass(mContext, AboutActivity.class);
			startActivity(intent);
		}
	};
	UmengUpdateListener updateListener = new UmengUpdateListener() {
		@Override
		public void onUpdateReturned(int updateStatus, UpdateResponse updateInfo) {
			switch (updateStatus) {
			case 0: // has update
				Log.i("--->", "callback result");
				UmengUpdateAgent.showUpdateDialog(mContext, updateInfo);
				break;
			case 1: // has no update
				Toast.makeText(mContext, "没有更新", Toast.LENGTH_SHORT).show();
				break;
			case 2: // none wifi
				Toast.makeText(mContext, "没有wifi连接， 只在wifi下更新", Toast.LENGTH_SHORT).show();
				break;
			case 3: // time out
				Toast.makeText(mContext, "超时", Toast.LENGTH_SHORT).show();
				break;
			case 4: // is updating
				/*
				 * Toast.makeText(mContext, "正在下载更新...", Toast.LENGTH_SHORT)
				 * .show();
				 */
				break;
			}

		}
	};

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

}
